package indi.lddsdu.util;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * Created by jack on 18/6/9.
 */
public class IndexUtil {
    /**
     *
     * @param indexPath
     * @param create
     * @return
     */
    public static IndexWriter getIndexWriter(String indexPath, boolean create) throws IOException {
        FSDirectory directory = FSDirectory.open(Paths.get(indexPath, new String[0]));
        Analyzer analyzer = new SmartChineseAnalyzer();
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
        if(create){
            indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        }else{
            indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        }
        IndexWriter writer = new IndexWriter(directory, indexWriterConfig);
        return writer;
    }
}
